/*
 * toc.js : little script for automatically numbering header, and building a
 * table of content. vi:ts=4:
 *
 * Written by T.Pierron, May 1, 2005.
 */

var toc = {
	levels: [],
	start:  2,
	build: function() {

		var doc = document.getElementById("bg").childNodes
		var toc = document.getElementById("toc")
		var li

		if (! toc) return

		for (var i = 0; i < doc.length; i ++)
		{
			if (doc[i].nodeName.search(/[hH]\d+/) != 0) continue

			var num = parseInt(doc[i].nodeName.substr(1)) - this.start
			var ol  = this.levels[num]

			if (num < 0 || num > 1) continue

			if (! ol)
			{
				this.levels[num] = ol = document.createElement("ol")
				if (num == 0) toc.appendChild(ol)
				else this.levels[num-1].appendChild(ol)
				if (num == 0) ol.className = "toplevel"
			}
			this.levels.length = num + 1

			var node = document.createElement("li")
			var link = "linkid" + i

			if (! doc[i].id) doc[i].id = link
			else link = doc[i].id

			node.innerHTML = "<a href='#" + link + "'>" + this.getContent(doc[i]) + "</a>"
			ol.appendChild(node)
		}
	},

	getContent: function(node) {

		var text = ""
		for (var i = 0; i < node.childNodes.length; i ++)
		{
			var sub = node.childNodes[i]

			if (sub.nodeValue) text += sub.nodeValue
		}
		return text
	}
}

